﻿//**************************
//开发人员：Morning Sun QQ:576830428  微信:MorningSun0125
//业务范围：网站开发、小程序开发、web应用开发,桌面程序开发,工控类软件开发等
//开发日期：2016年1月10日
//版本号：Version 3.0 CopyRight 2011-2029
//**************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace FileServer
{
    public class RSA
    {
        #region 解密私钥
        /// <summary>
        /// 解密私钥
        /// </summary>
        private static string xmlPrivateKey = "<RSAKeyValue><Modulus>yST4nxWGb+qZh7dD+UrZo1fCTy4RFWX03Jm1sBvjdVqc6u06HUNDVBa4TCvISqzdICDDkM5R1O1hkF0QA3qQ8CvVgUSd+yekIuKdXmUuJ5PxvdUfIMFjIRvGTksJK2x0tNYZGilwmbMNn9TCO0kzj4FQbB9wYcvGEK928gzhPpU=</Modulus><Exponent>AQAB</Exponent><P>+zpfmm79LuuZrtpgcZVNOXUk6ZKDsgONUdcLuEgn3NLCzR3B9Gb+70jQ0CiL+Rdb9M6u/f6SusWy5AqGy93Dpw==</P><Q>zPcPZ0hWRWa5dz7/b3iEezuf19RcZTnKyTE6BoQnDINrRgTb53hrzZQiAoqXAh9UrSvLUFSqO24/vwA0o4NjYw==</Q><DP>l0LfOxmmYTDDeG7NcLXnKDgrAGMR3gWbefITgWIYdjGDzoIjPGcjCal66EnxdQmmeTBUVmOHruLHwdvAq1Cbsw==</DP><DQ>SsZTS0W7m4QXY2aLP7qC8oZVESM7Nf64eV/JIHlz2X/UUe4R38cx5QUGx1CSOMb/WFHi0YKhvB7DiMXlbkXPqw==</DQ><InverseQ>XwT9bzqiBg3dNpYdaoEuTCX+Dw/lS6UJ/OzQe6G/hei21WT7UzYm8FRA6jGm5hnKAEmkD7QhkA5OQfdDUbnhwQ==</InverseQ><D>i2epPDBh7TztzUaxwJwkvHV9QP1Pb8Iq851Ac97QRSxY5uUlStSsXCIm9vu9JCwsUTED+V9VDbq7zJ8DEzYmLmI2e0WMsJuyTsR7wrD17C3/zODi6UZDS8zM6AWxxK7HQGTtCfeEcMTFtZJgvdqHMDwfR6pDhZzTWSoI8c000p0=</D></RSAKeyValue>";
        #endregion


        #region 私钥解密
        /// <summary>
        /// 私钥解密
        /// </summary>
        /// <param name="m_strDecryptString"></param>
        /// <returns></returns>
        public static string RSADecrypt(string m_strDecryptString)
        {
            return RSADecrypt(xmlPrivateKey, m_strDecryptString);
        }
        #endregion


        #region 解密
        /// <summary>   
        /// RSA解密   
        /// </summary>   
        /// <param name="xmlPrivateKey"></param>   
        /// <param name="m_strDecryptString"></param>   
        /// <returns></returns>   
        public static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(xmlPrivateKey);
            byte[] rgb = Convert.FromBase64String(m_strDecryptString);
            byte[] bytes = provider.Decrypt(rgb, false);
            return new UnicodeEncoding().GetString(bytes);
        }
        #endregion


        #region 加密
        /// <summary>   
        /// RSA加密   
        /// </summary>   
        /// <param name="xmlPublicKey"></param>   
        /// <param name="m_strEncryptString"></param>   
        /// <returns></returns>   
        public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(xmlPublicKey);
            byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
            return Convert.ToBase64String(provider.Encrypt(bytes, false));
        }
        #endregion
    }
}
